package com.bawei.module_mine.ui


import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.lib_storage.database.FavoriteManager
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_mine.adapter.CollectVideoAdapter
import com.bawei.module_mine.databinding.ActivityCollectBinding
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.launch

@Route(path = "/mine/CollectActivity")
class CollectActivity : BaseActivity() {

    private lateinit var binding: ActivityCollectBinding
    private lateinit var collectAdapter: CollectVideoAdapter
    private val userId = AuthMMKV.uId.toString() // 使用实际的用户ID


    override fun initView() {

        // 设置返回按钮
        binding.ivCollectBack.setOnClickListener {
            finish()
        }

        // 初始化RecyclerView
        binding.rvCollectList.layoutManager = LinearLayoutManager(this)
        collectAdapter = CollectVideoAdapter(
            coroutineScope = lifecycleScope,
            userId = userId,
            onItemClick = { favoriteVideo ->
                // 点击收藏项的处理，可以跳转到视频详情页
                ToastUtils.showLong("点击了: ${favoriteVideo.title}")
            },
            onUnfavorite = { favoriteVideo, position ->
                // 取消收藏的处理
                collectAdapter.removeItem(position)
                ToastUtils.showShort("已取消收藏: ${favoriteVideo.title}")
                // 检查是否还有收藏内容
                if (collectAdapter.itemCount == 0) {
                    binding.tvCollectEmpty.visibility = View.VISIBLE
                    binding.rvCollectList.visibility = View.GONE
                }
            }
        )
        binding.rvCollectList.adapter = collectAdapter
    }

    override fun getLayoutRootView(): View {
        binding = ActivityCollectBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initData() {
        lifecycleScope.launch {
            try {
                android.util.Log.d("CollectActivity", "Loading favorites for userId: $userId")
                val favoriteList = FavoriteManager.getAllFavorites(userId)
                android.util.Log.d("CollectActivity", "Found ${favoriteList.size} favorites")

                if (favoriteList.isEmpty()) {
                    // 没有收藏内容
                    android.util.Log.d("CollectActivity", "No favorites found, showing empty state")
                    binding.tvCollectEmpty.visibility = View.VISIBLE
                    binding.rvCollectList.visibility = View.GONE
                } else {
                    // 有收藏内容
                    android.util.Log.d("CollectActivity", "Found favorites, showing list")
                    binding.tvCollectEmpty.visibility = View.GONE
                    binding.rvCollectList.visibility = View.VISIBLE
                    collectAdapter.setData(favoriteList)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                android.util.Log.e("CollectActivity", "Error loading favorites", e)
                ToastUtils.showShort("加载收藏失败")
                binding.tvCollectEmpty.visibility = View.VISIBLE
                binding.rvCollectList.visibility = View.GONE
            }
        }
    }


}